var express = require('express');
var router = express.Router();
let multiparty = require('multiparty');
let path = require('path');
let { listModel, userModel ,cartModel} = require('../db/index')
/* GET home page. */
router.post('/zhu',async function(req, res, next) {
  let body = req.body;
  let data = await userModel.find({username:body.username})
  if (data.length>0) {
    res.send({
      code:400,
      msg:'用户名已存在'
    })
    return;
  }else{
    await userModel.create(body)
    res.send({
      code:200,
    })
  }
  
});
router.post('/login',async function(req, res, next) {
  let body = req.body;
  let data = await userModel.find({username:body.username,password:body.password})
  res.send({
    data:data, 
  })
});
router.get('/my',async function(req, res, next) {
  let name = req.query.name;
  console.log(name);
  let data = await userModel.find({username:name})
  console.log(data);
  res.send({
    data:data, 
  })
});
router.post('/upload', (req, res) => {
 var form = new multiparty.Form();
  form.uploadDir = 'public'
  form.parse(req, (err, fields, files) => {
    console.log(files);
    const file = files.file[0];
    const publicUrl = `/public/${path.basename(file.path)}`;
    res.send({ url:'http://localhost:3000'+ publicUrl }); // 返回可访问的URL
  });
});
router.get('/indexu',async function(req, res, next) {
  let id = req.query.id;
  console.log(id);
  let data = await userModel.find({_id:id})
  res.send({
    data:data,
  })
});
router.post('/update',async function(req, res, next) {
  let body = req.body;
  await userModel.updateOne({_id:body.id},body) 
  res.send({
    code:200, 
  })
});
router.post('/upimg',async function(req, res, next) {
var form = new multiparty.Form();
  form.uploadDir = 'public'
  form.parse(req, (err, fields, files) => {
    const file = files.avatar[0];
    const publicUrl = `/public/${path.basename(file.path)}`;
    console.log(publicUrl);
    console.log(file.path);
    
    
    res.send({ url:'http://localhost:3000'+ publicUrl }); // 返回可访问的URL
  }); 
})
router.get('/mai',async function(req, res, next) {
    let id = req.query.id
    let data  = await listModel.find({user_id:id})
    res.send({
      data
    })
});
router.get('/delete',async function(req, res, next) {
    let id = req.query.id
    await listModel.deleteOne({_id:id})
    res.send({
      code:200
    })
});
router.get('/sell',async function(req, res, next) {
    let id = req.query.id
    let data = await cartModel.find().populate('cart_id')
    let zhong = []
    data.forEach((item)=>{
      if(item.cart_id.user_id == id && item.state == 1){
        zhong.push(item.cart_id)
      }
    })
   res.send({
    zhong
   })
   
});
router.get('/bought',async function(req, res, next) {
    let id = req.query.id
    let data = await cartModel.find().populate('cart_id')
    let zhong = []
    data.forEach((item)=>{
      if(item.user_id == id && item.state == 1){
        zhong.push(item.cart_id)
      }
    })
    res.send({
      zhong 
    })
});
router.post('/cartadd',async function(req, res, next) {
   await cartModel.create(req.body)
   res.send({
    code:200 
   })
});
module.exports = router;
